package com.linkage.util.password.des3;
//package Acme.Crypto;

public class Des3{
	static  final byte key[]=
	{
		0x01,0x23,0x45,0x67,(byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef,
		(byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,0x76,0x54,0x32,0x10,
        	(byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef,0x01,0x23,0x45,0x67 
	};
	static final byte Octes[]=
	{
		'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
	};
	Des3Cipher d3;

	public Des3()
	{
		d3 = new Des3Cipher(key);
	}

	public void Encrypt(byte[] inblock, byte[] outblock)
	{	
		byte input[]=new byte[64];
		byte output[]=new byte[64];
		
		System.arraycopy(inblock,0,input,0,inblock.length);
		d3.encrypt(input,0,output,0);
		copyToOctes(output,outblock,8);
	}

	public String Encrypt(String inblock)
	{
		byte input[]=new byte[64];
		byte output[]=new byte[64];
		
		String outstring = "";
		byte temp[] = new byte[64];
		System.arraycopy(inblock.getBytes(),0,input,0,inblock.length());
		d3.encrypt(input,0,output,0);
		copyToOctes(output,temp,8);
		outstring = new String(temp);
		return outstring;
	}

	public void Decrypt(byte[] inblock, byte[] outblock)
	{	
		byte input[]=new byte[64];
		byte output[]=new byte[64];
		
		copyFromOctes(inblock,input,inblock.length);
		d3.decrypt(input,0,output,0);
		System.arraycopy(output,0,outblock,0,output.length);
	}
	
	public String Decrypt(String inblock)
	{
		byte input[]=new byte[64];
		byte output[]=new byte[64];
		String outstring = "";
		byte temp[] = new byte[64];
		System.arraycopy(inblock.getBytes(),0,temp,0,inblock.length());
		copyFromOctes(temp,input,inblock.length());
		d3.decrypt(input,0,output,0);
		outstring = new String(output);
		return outstring;
	}	

	static void copyToOctes(byte[] instr,byte[] outstr, int len)
	{
		int i;
		for(i=0;i<len;i++)
		{
			outstr[i*2]=Octes[((instr[i] >>> 4)& 0x0f)];
			outstr[i*2+1]=Octes[(instr[i] & 0x0f)];
		}	
	}

	static void copyFromOctes(byte[] instr, byte[] outstr, int len)
	{
		int i,j,k;
		for(i=0;i<(len/2);i++)
		{
			if ( instr[i*2] >= 'a' ) 
				j = instr[i*2] - 'a' + 10;
                	else 
				j = instr[i*2] - '0';

        		if ( instr[i*2+1] >= 'a' )
				k = instr[i*2+1] - 'a' + 10;
                	else 
				k = instr[i*2+1] - '0';

 	               	outstr[i] = (byte)(j * 16 +k);
		}
	}

	static void printUseOctes(byte[] str,int len)
	{
		char Octes[]=
		{
			'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
		};
		int i;
		for(i=0;i<len;i++)
		{
			System.out.print("0x" + Octes[((str[i] >>> 4)& 0x0f)] + Octes[(str[i] & 0x0f)] + " ");
		}
		System.out.println();
	}
	public static void main(String[] args) {
		Des3 de3 = new Des3();
		String password = "hello";
		String encrypt = de3.Encrypt(password);
		String decrypt = de3.Decrypt(encrypt);
		System.out.println("password:	"+password);
		System.out.println("encrypt:	"+encrypt);
		System.out.println("decrypt:	"+decrypt);
		System.out.println("pwddecry:	"+de3.Decrypt(password));
		
		byte[] pb = new byte[64];
		byte[] eb = new byte[64];
		byte[] db = new byte[64];
		
		try {
			System.arraycopy(password.getBytes(),0,pb,0,(password.getBytes()).length);
			de3.Encrypt(pb,eb);
			de3.Decrypt(eb,db);
			System.out.println("\npwdC:	"+new String(pb));
			System.out.println("\npwdE:	"+new String(eb));
			System.out.println("\npwdD:	"+new String(db));
		}catch (Exception d) {
		}
	}

}
